package com.stm.bi.core.util;


import com.stm.bi.enums.DateType;
import com.stm.bi.model.DateRange;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

import static com.stm.bi.constant.PlanConstants.*;

/**
 * @Description:
 * @Author: bo
 * @Date: 2023/04/18 15:20
 */
public class DateUtils {

    private final static List<Integer> monthList1 = Arrays.asList(1, 3, 5, 7, 8, 10, 12);
    private final static List<Integer> monthList2 = Arrays.asList(4, 6, 9, 11);
    //yyyy-MM

    /**
     * 获取当月天数
     * @param date 日期字符串
     */
    public static int getMonthDays(String date){

        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(5,7));


        if(monthList1.contains(month)){
            return 31;
        }else if (monthList2.contains(month)) {
            return 30;
        } else if (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) && month == 2) {
            return 29;
        } else if (year % 4 != 0 || month != 2) {
            return 28;
        }
        return 0;
    }

    /**
     * 判断是否闰年
     * @param year
     */
    public static boolean isLeapYear(int year){
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    /**
     * 获取星期几
     * @param date
     * @return 星期
     */
    public static int getWeekNumber(String date){

        int year = getYear(date);
        int month = getMonth(date);;
        int day = getDay(date);;
        if (month == 1 || month == 2) {
            month += 12;
            year--;
        }

        int i = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400 + 1) % 7;

        return i == 0 ? 7 : i;
    }

    /**
     * 获取年份
     * @param date
     */
    public static int getYear(String date){
        return LocalDate.parse(date).getYear();
    }


    public static int getMonth(String date){
        return LocalDate.parse(date).getMonth().getValue();
    }

    public static int getDay(String date){
        return LocalDate.parse(date).getDayOfMonth();
    }

    public static Boolean isWeekend(String date) {

        int weekCalculate = getWeekNumber(date);

        if(weekCalculate == 6 || weekCalculate == 7){
            return true;
        }

        return false;
    }

    public static String getLastMonth(String date){
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(5,7));
        if(month==1){
            month =12;
            year --;
        }else{
            month--;
        }
        return  year + "-" + addZero(month);
    }

    public static String getLastYear(String date){
        int year = Integer.parseInt(date.substring(0,4));
        return (year-1)+"";
    }

    public static String getLastWeek(String date,int weekCyc,int monthCyc){
        date = getWeekFirstAndEnd(date, weekCyc, monthCyc).getStartDate();
        date = getLastDay(date);
        return getWeekFirstAndEnd(date, weekCyc, monthCyc).getStartDate();
    }

    public static String getLastWeek(String date){
        String year = date.substring(0, 4);
        String weekNum = date.substring(5, 7);
        if("01".equals(weekNum)){
            year = (Integer.parseInt(year) - 1) + "";
            return year + "-" + addZero(getYearWeekNum(year));
        }else {
            return year + "-" + addZero(Integer.parseInt(weekNum) - 1);
        }
    }


    public static String getLastYearMonth(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        return (year - 1) + "-" + addZero(month);
    }

    public static String getNextYearMonthDay(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        String monthDay = date.substring(5, 10);
        return (year + 1) + "-" + monthDay;
    }

    public static String getThisMonth(String date) {
        return date.substring(0, 7);
    }

    public static String getNextMonth(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        if (month == 12) {
            return (year + 1) + "-01";
        }else{
            month++;
        }
        return  year + "-" + addZero(month);
    }

    public static String getNextYear(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        return  String.valueOf(++year);
    }

    public static String getNextWeek(String date) {
        String year = date.substring(0, 4);
        String weekNum = date.substring(5, 7);

        if( getYearWeekNum(year) == Integer.parseInt(weekNum)){
            year = (Integer.parseInt(year) + 1) + "";
            return year + "-01";
        }else {
            return year + "-" + addZero(Integer.parseInt(weekNum) + 1);
        }
    }

    public static String getNextWeek(String date, int weekCyc, int monthCyc) {
        date = getWeekFirstAndEnd(date, weekCyc, monthCyc).getEndDate();
        date = getNextDay(date);
        return getWeekFirstAndEnd(date, weekCyc, monthCyc).getStartDate();
    }

    public static String getWeek(String date){
        return date.substring(0,4) + "-" + addZero(getYearWeek(date));
    }

    //根据日期算出是当前年的第几周
    public static int getYearWeek(String date){
        //找到第一个周一,是年中的第几天
        String firstMonDay = date.substring(0,4) + "-01-01";
        int days = 1;
        while(true){
            int weekNumber = getWeekNumber(firstMonDay);
            if(weekNumber == 1){
                break;
            }else {
                firstMonDay = getNextDay(firstMonDay);
                days ++;
            }
        }

        //算出第一个周一与今天差几天
//        int allDays = 0;
        long between = ChronoUnit.DAYS.between(LocalDate.parse(firstMonDay), LocalDate.parse(date));
//        allDays = between;

        return (between < 0 ? -1 : (int) between /7) + 1 +  (days > 1 ? 1 : 0);

    }

    public static int getYearWeekNum(String year){
        return getYearWeek(year + "-12-31");
    }

    public static String getCurrentDate(){
        return LocalDate.now().toString();

    }

    public static String addZero(Integer s){
        return s>9?""+s:"0"+s;
    }

    public static String getNextDay(String date){
        int day = getDay(date);
        String nextMonth = getNextMonth(date);
        int days = getMonthDays(date);

        if (day + 1 > days) {
            return nextMonth + "-01";
        } else {
            return date.substring(0, 8) + addZero(day + 1);
        }
    }

    public static String getNextHour(String date) {
        String date1 = date.substring(0, 10);
        String date2 = date.substring(11, 13);
        if (Objects.equals(date2, "23")) {
            return getNextDay(date1) + "T00:00:00.000Z";
        } else {
            return date1 + "T" + addZero(Integer.parseInt(date2) + 1) + ":00:00.000Z";
        }
    }

    public static String getLastDay(String date) {
        int day = getDay(date);
        String lastMonth = getLastMonth(date);
        int days = getMonthDays(lastMonth);
        if (day - 1 == 0) {
            return lastMonth + "-" + days;
        } else {
            return date.substring(0, 8) + addZero(day - 1);
        }
    }

    public static DateRange getYearFirstAndEnd(String date, int yearCyc) {
        DateRange dateRange = new DateRange();
        int year = Integer.parseInt(date.substring(0, 4));
        int month = yearCyc;
        if (date.length() >= 7) {
            month = Integer.parseInt(date.substring(5, 7));
        }
        int lastMonth = yearCyc - 1 > 0 ? yearCyc - 1 : 12;
        if (month >= yearCyc) {
            if (yearCyc == 1) {
                dateRange.setStartDate(year + "-" + addZero(yearCyc));
                dateRange.setEndDate((year) + "-" + addZero(lastMonth));
            }else {
                dateRange.setStartDate(year + "-" + addZero(yearCyc));
                dateRange.setEndDate((year + 1) + "-" + addZero(lastMonth));
            }
        } else {
            dateRange.setStartDate((year - 1) + "-" + addZero(yearCyc));
            dateRange.setEndDate(year + "-" + addZero(lastMonth));
        }
        return dateRange;
    }

    public static DateRange getMonthFirstAndEnd(String date, int monthCyc) {
        DateRange dateRange = new DateRange();
        int monthDay = getMonthDays(date);
        if (date.length() >= 10) {
            int day = getDay(date);
            int month = getMonth(date);
            int year = getYear(date);
            if (day >= monthCyc) {
                dateRange.setStartDate(year + "-" + addZero(month) + "-" + addZero(monthCyc));
                if (monthCyc == 1) {
                    dateRange.setEndDate(year + "-" + addZero(month) + "-" + addZero(monthDay));
                }else {
                    dateRange.setEndDate(year + "-" + addZero(month + 1) + "-" + addZero(monthCyc - 1));
                }
            }else {
                dateRange.setStartDate(getLastMonth(year + "-" + addZero(month)) + "-" + addZero(monthCyc));
                dateRange.setEndDate(year + "-" + addZero(month) + "-" + addZero(monthCyc - 1));
            }
        }else {
            String yearMonth = date.substring(0,7);
            dateRange.setStartDate(yearMonth + "-" + addZero(monthCyc));
            if(monthCyc==1){
                dateRange.setEndDate(yearMonth + "-" + addZero(monthDay));
            }else {
                dateRange.setEndDate(getNextMonth(yearMonth) + "-" + addZero(monthCyc - 1));
            }
        }
        return dateRange;
    }

    public static String getWeekFirstDay(String week) {

        String year = week.substring(0,4);
        int weekNumber = Integer.parseInt(week.substring(5,7)) ;

        //第一周直接返回1月1号
        if(weekNumber == 1){
            return year + "-01-01";
        }else {
            //获取1月1号是周几
            int weekNum = getWeekNumber(year + "-01-01");

            int days = (weekNumber - 1) * 7 + 1;
            if(getWeekNumber(year + "-01-01" ) == 1){
                return getDateByDays(year, days);
            }else {
                return getDateByDays(year, days - weekNum + 1) ;
            }
        }
    }

    public static DateRange getWeekFirstAndEnd(String week) {
        DateRange dateRange = new DateRange();
        String firstDay = getWeekFirstDay(week);
        String nextWeek = getNextWeek(week);
        String endDay = getLastDay(getWeekFirstDay(nextWeek));
        return dateRange.setStartDate(firstDay).setEndDate(endDay);
    }

    public static int getWeekDays(String week) {
        DateRange weekFirstAndEnd = getWeekFirstAndEnd(week);
        String startDate = weekFirstAndEnd.getStartDate();
        String endDate = weekFirstAndEnd.getEndDate();
        int days = 1;
        while(startDate.compareTo(endDate) != 0){
            days++;
            startDate = getNextDay(startDate);
        }
        return days;
    }

    public static String getDateByDays(String year, int days){
        int day = days;
        for(int i = 1; i <= 12; i++){
            if(monthList1.contains(i)){
                day = days - 31;
                if (day <= 0){
                    return year + "-" + addZero(i) + "-" + addZero(days);
                }
                days -= 31;
            }else if(monthList2.contains(i)){
                day = days - 30;
                if (day <= 0){
                    return year + "-" + addZero(i) + "-" + addZero(days);
                }
                days -= 30;
            }else if(isLeapYear(Integer.parseInt(year))){
                day = days - 29;
                if (day <= 0){
                    return year + "-" + addZero(i) + "-" + addZero(days);
                }
                days -= 29;
            }else {
                day = days - 28;
                if (day <= 0){
                    return year + "-" + addZero(i) + "-" + addZero(days);
                }
                days -= 28;
            }
        }
        return year;
    }


    public static DateRange getWeekFirstAndEnd(String date, int weekCyc, int monthCyc) {
        //Utils.
        DateRange dateRange = new DateRange();
        int day = getDay(date);
        int month = getMonth(date);
        int year = getYear(date);
        int weekNumber = getWeekNumber(date);
        int monthDay = getMonthDays(date);
        int lastMonthDay = getMonthDays(getLastMonth(date));
        int firstDay, lastDay;
        int lastDif = 6;

        firstDay = weekNumber >= weekCyc? day - weekNumber + weekCyc:day - 7 - weekNumber + weekCyc;

        if(day >= monthCyc && firstDay <= monthCyc ){
            int firstDayTemp = firstDay;
            firstDay = monthCyc;
            lastDif = 6 - (firstDay - firstDayTemp);
        }

        if(firstDay <= 0) {
            firstDay += lastMonthDay;
        }
        if(day < monthCyc) {
            monthDay = lastMonthDay;
        }

        lastDay = firstDay + lastDif > monthDay ?
                Math.min(firstDay + lastDif- monthDay,monthCyc -1) :
                (firstDay + lastDif >= monthCyc && firstDay < monthCyc)?monthCyc - 1:firstDay + lastDif;
        lastDay = lastDay == 0 ? monthDay:lastDay;

        if(firstDay>lastDay){
            if(day >= monthCyc) {
                dateRange.setStartDate(year + "-" + addZero(month) + "-" + addZero(firstDay));
                dateRange.setEndDate(getNextMonth(year + "-" + addZero(month)) + "-" + addZero(lastDay));
            }else {
                dateRange.setStartDate(getLastMonth(year + "-" + addZero(month)) + "-" + addZero(firstDay));
                dateRange.setEndDate(year + "-" + addZero(month) + "-" + addZero(lastDay));
            }

        } else {
            dateRange.setStartDate(year + "-" + addZero(month) + "-" + addZero(firstDay));
            dateRange.setEndDate(year + "-" + addZero(month) + "-" + addZero(lastDay));
        }

        return dateRange;
    }


    public static DateRange getNatureWeekFirstAndEnd(String date, int weekCyc) {
        //Utils.
        DateRange dateRange = new DateRange();
        int weekNumber = getWeekNumber(date);

        int flag = weekNumber - weekCyc >= 0 ? weekNumber - weekCyc : weekNumber - weekCyc + 7;

        String startDate = date;
        String endDate = date;
        for (int i = 0; i < flag; i++) {
            startDate = getLastDay(startDate);
        }

        for (int i = 0; i < 6 - flag; i++) {
            endDate = getNextDay(endDate);
        }

        dateRange.setStartDate(startDate).setEndDate(endDate);

        return dateRange;
    }

    public static DateRange getDayFirstAndEnd(String date, int yearCyc, int monthCyc) {
        if (date.length() >= 10) {
            return new DateRange().setStartDate(date.substring(0, 10)).setEndDate(date.substring(0, 10));
        } else if (date.length() >= 7) {
            return getMonthFirstAndEnd(date, monthCyc);
        } else if (date.length() >= 4) {
            DateRange dateRange = getYearFirstAndEnd(date, yearCyc);
            DateRange start = getMonthFirstAndEnd(dateRange.getStartDate(), monthCyc);
            DateRange end = getMonthFirstAndEnd(dateRange.getEndDate(), monthCyc);
            return new DateRange().setStartDate(start.getStartDate()).setEndDate(end.getEndDate());
        } else {
            return new DateRange();
        }
    }

    public static DateRange getHourFirstAndEnd(String date, int yearCyc, int monthCyc) {
        if (date.length() >= 10) {
            return new DateRange().setStartDate(date + "T00:00:00.000Z").setEndDate(date + "T23:00:00.000Z");
        } else if (date.length() >= 7) {
            DateRange dateRange = getMonthFirstAndEnd(date, monthCyc);
            return new DateRange().setStartDate(dateRange.getStartDate() + "T00:00:00.000Z").setEndDate(dateRange.getEndDate() + "T23:00:00.000Z");
        } else if (date.length() >= 4) {
            DateRange dateRange = getDayFirstAndEnd(date, yearCyc, monthCyc);
            return new DateRange().setStartDate(dateRange.getStartDate() + "T00:00:00.000Z").setEndDate(dateRange.getEndDate() + "T23:00:00.000Z");
        } else {
            return new DateRange();
        }
    }

    public static String getMonthCyc(String date,int monthCyc){
        if(getDay(date)>=monthCyc){
            return getThisMonth(date);
        }else{
            return getLastMonth(date);
        }

    }

    public static List<String> getMonthList(String date,int yearCyc) {
        DateRange dateRange = getYearFirstAndEnd(date, yearCyc);
        String start = dateRange.getStartDate();
        String end = dateRange.getEndDate();
        List<String> l = new ArrayList<>();
        while (start.compareTo(end) <= 0) {
            l.add(start);
            start = getNextMonth(start);
        }
        return l;
    }

    public static List<DateRange> getWeekList(String date, int weekCyc, int monthCyc) {
        String month = getCycMonth(date, monthCyc);
        date = month + "-" + addZero(monthCyc);
        String flag = getCycMonth(date, monthCyc);
        List<DateRange> list = new ArrayList<>();
        while (flag.equals(month)) {
            DateRange dateRange = getWeekFirstAndEnd(date, weekCyc, monthCyc);
            list.add(dateRange);
            date = getNextDay(dateRange.getEndDate());
            flag = getCycMonth(date, monthCyc);
        }
        return list;
    }

    public static List<DateRange> getWeekList(DateRange date, int weekCyc, int monthCyc) {
        String endDate = date.getEndDate();
        String flag = date.getStartDate();
        List<DateRange> list = new ArrayList<>();
        while (flag.compareTo(endDate) < 0) {
            DateRange dateRange = getWeekFirstAndEnd(flag, weekCyc, monthCyc);
            list.add(dateRange);
            flag = getNextDay(dateRange.getEndDate());
        }
        return list;
    }

    public static List<String> getDateList(String firstDate, String endDate, String cycle) {
        List<String> l = new ArrayList<>();
        if (Objects.equals(cycle, TYPE_DAY)) {
            if(firstDate.length()< 10){
                firstDate = firstDate.substring(0,7) + "-01";
            }
            if(endDate.length() < 10){
                endDate = endDate.substring(0,7) + "-" + getMonthDays(endDate.substring(0,7));
            }
            while (firstDate.compareTo(endDate) <= 0) {
                l.add(firstDate);
                firstDate = getNextDay(firstDate);
            }
        } else if (Objects.equals(cycle, TYPE_MONTH)) {
            if(firstDate.length() < 7){
                firstDate = firstDate.substring(0,4) + "-01";
            }
            if(endDate.length() < 7){
                endDate = endDate.substring(0,4) + "-12";
            }
            while (firstDate.compareTo(endDate) <= 0) {
                l.add(firstDate.substring(0,7));
                firstDate = getNextMonth(firstDate);
            }
        } else if (Objects.equals(cycle, TYPE_HOUR)) {
            while (firstDate.compareTo(endDate) <= 0) {
                l.add(firstDate);
                firstDate = getNextHour(firstDate);
            }
        } else if (Objects.equals(cycle, TYPE_WEEK)){

            if(firstDate.length() < 7){
                firstDate = firstDate.substring(0,4) + "-01";
            }else if(firstDate.length() < 10){
                firstDate = getWeek(firstDate + "-01");
            }else {
                firstDate = getWeek(firstDate);
            }

            if(endDate.length() < 7){
                endDate = endDate.substring(0,4) + "-" + getYearWeekNum(endDate.substring(0,4));
            }else if(endDate.length() < 10){
                endDate = getWeek(endDate + "-" + addZero(getMonthDays(endDate.substring(0,7))));
            }else {
                endDate = getWeek(endDate);
            }

            while (firstDate.compareTo(endDate) <= 0){
                l.add(firstDate);
                firstDate = getNextWeek(firstDate);
            }
        }
        return l;
    }

    public static List<String> getReportDateList(String firstDate, String endDate, String cycle) {
        List<String> l = new ArrayList<>();
        if (Objects.equals(cycle, TYPE_DAY)) {
            if(firstDate.length()< 10){
                firstDate = firstDate.substring(0,7) + "-01";
            }
            if(endDate.length() < 10){
                endDate = endDate.substring(0,7) + "-" + getMonthDays(endDate.substring(0,7));
            }
            while (firstDate.compareTo(endDate) <= 0) {
                l.add(firstDate);
                firstDate = getNextDay(firstDate);
            }
        } else if (Objects.equals(cycle, TYPE_MONTH)) {
            if(firstDate.length() < 7){
                firstDate = firstDate.substring(0,4) + "-01";
            }
            if(endDate.length() < 7){
                endDate = endDate.substring(0,4) + "-12";
            }
            while (firstDate.compareTo(endDate) <= 0) {
                l.add(firstDate.substring(0,7));
                firstDate = getNextMonth(firstDate);
            }
        } else if (Objects.equals(cycle, TYPE_HOUR)) {
            while (firstDate.compareTo(endDate) <= 0) {
                l.add(firstDate);
                firstDate = getNextHour(firstDate);
            }
        } else if (Objects.equals(cycle, TYPE_WEEK)){

            if(firstDate.length() < 7){
                firstDate = firstDate.substring(0,4) + "-01";
            }else if(firstDate.length() < 10){
                firstDate = getWeek(firstDate + "-01");
            }else {
                firstDate = getWeek(firstDate);
            }

            if(endDate.length() < 7){
                endDate = endDate.substring(0,4) + "-" + getYearWeekNum(endDate.substring(0,4));
            }else if(endDate.length() < 10){
                endDate = getWeek(endDate + "-" + addZero(getMonthDays(endDate.substring(0,7))));
            }else {
                endDate = getWeek(endDate);
            }

            while (firstDate.compareTo(endDate) <= 0){
                l.add(getWeekFirstDay(firstDate));
                firstDate = getNextWeek(firstDate);
            }
        } else if (Objects.equals(cycle, TYPE_YEAR)){
            while (firstDate.compareTo(endDate) <= 0) {
                l.add(firstDate.substring(0,4));
                firstDate = getNextYear(firstDate);
            }
        }
        return l;
    }


    public static List<String> getHistoryDateList(String startDate, String endDate, String dateType, Integer delay) {

        List<String> dateList = DateUtils.getReportDateList(startDate, endDate, dateType);

        String nowDate = LocalDate.now().minusDays(delay).toString();
        if(TYPE_MONTH.equals(dateType)){
            dateList.removeIf(d -> d.compareTo(DateUtils.getCycMonth(nowDate, 1)) > 0);
        }else {
            dateList.removeIf(d -> d.compareTo(nowDate) > 0);
        }

        return dateList;
    }

    /**
     * 获取周期年
     *
     * @param date
     * @param yearCyc
     */
    public static String getCycYear(String date, int yearCyc) {
        DateRange dateRange = getYearFirstAndEnd(date, yearCyc);
        return dateRange.getStartDate().substring(0, 4);
    }

    /**
     * 获取周期月
     * @param date
     * @param monthCyc
     */
    public static String getCycMonth(String date,int monthCyc){
        if(getDay(date)>=monthCyc){
            return getThisMonth(date);
        }else{
            return getLastMonth(date);
        }
    }

    public static String getName(String date){
        String year;
        String month;
        String day;
        if(date.length()>=10){
            year = date.substring(0,4);
            month = date.substring(5,7);
            day = date.substring(8,10);
            return year + "年" + Integer.parseInt(month) + "月" + Integer.parseInt(day) + "日";
        }else if(date.length()>=7){
            year = date.substring(0,4);
            month = date.substring(5,7);
            return year + "年" + Integer.parseInt(month) + "月";
        } else if (date.length() >= 4) {
            year = date.substring(0, 4);
            return year + "年";
        } else {
            return "";
        }
    }

    public static String getNameByCycle(String date, String cycle) {
        String year;
        String month;
        String day;
        String hour;
        if (Objects.equals(TYPE_DAY, cycle) || Objects.equals(TYPE_WEEK, cycle)) {
            month = date.substring(5, 7);
            day = date.substring(8, 10);
            return Integer.parseInt(month) + "月" + Integer.parseInt(day) + "日";
        } else if (Objects.equals(TYPE_MONTH, cycle)) {
            month = date.substring(5, 7);
            return Integer.parseInt(month) + "月";
        } else if (Objects.equals(TYPE_HOUR, cycle)) {
            if (date.length() < 16) {
                hour = date.substring(11, 13) + ":00~" + getNextHour(date).substring(11, 13) + ":00";
            } else {
                hour = date.substring(11, 16) + "~" + getNextHour(date).substring(11, 16);
            }
            return hour;
        } else {
            return "";
        }
    }


    public static String getWeekName(String week) {
        int weekNum = Integer.parseInt(week.substring(5,7));
        return "第" + weekNum + "周";
    }

    public static String getWeekFullColumn(String week) {
        DateRange dateRange = DateUtils.getWeekFirstAndEnd(week);
        return DateUtils.getWeekName(week) + "\n" + dateRange.getStartDate().substring(5, 10).replace("-", ".") + "-" + dateRange.getEndDate().substring(5, 10).replace("-", ".");
    }

    public static String getWeekFullColumnByDate(String date) {
        String week = getWeek(date);
        DateRange dateRange = DateUtils.getWeekFirstAndEnd(getWeek(date));
        return DateUtils.getWeekName(week) + "\n" + dateRange.getStartDate().substring(5, 10).replace("-", ".") + "-" + dateRange.getEndDate().substring(5, 10).replace("-", ".");
    }


    public static void main(String[] args) {
        List<String> weekList = getDateList("2023-01", "2023-12", TYPE_WEEK);;
        List<String> weekFirstDay = new ArrayList<>();
        weekList.forEach(s -> {
            String firstDay = getWeekFirstDay(s);

            weekFirstDay.add(firstDay);
            String week = getWeek(firstDay);
            System.out.println( s + ";" + firstDay + ";" + week + ";结果正确：" + Objects.equals(s, week));
        });

//        getYearWeek("2023-12-31");
    }

    public static String getDateStrByDateType(LocalDateTime localDateTime, DateType dateType) {
        String date = localDateTime.toLocalDate().toString();
        if(TYPE_YEAR.equals(dateType.getValue())){
            date = localDateTime.getYear() + "";
        }else if(TYPE_MONTH.equals(dateType.getValue())){
            date = localDateTime.getYear() + "-" + addZero(localDateTime.getMonthValue());
        }else {
            date = date;
        }
        return date;
    }


    public static LocalDateTime parseStringToLocalDateTime(String dateStr, DateType dateType) {
        LocalDate localDate;

        switch (dateType.getValue()) {
            case TYPE_YEAR:
                // 假设字符串格式为 "yyyy"
                localDate = LocalDate.of(Integer.parseInt(dateStr), 1, 1);
                break;
            case TYPE_MONTH:
                // 假设字符串格式为 "yyyy-MM"
                String[] yearMonth = dateStr.split("-");
                int year = Integer.parseInt(yearMonth[0]);
                int month = Integer.parseInt(yearMonth[1]);
                localDate = LocalDate.of(year, month, 1);
                break;
            case TYPE_DAY, TYPE_WEEK:
                // 假设字符串格式为 "yyyy-MM-dd"
                localDate = LocalDate.parse(dateStr);
                break;
            default:
                throw new IllegalArgumentException("Unsupported date type: " + dateType);
        }

        return LocalDateTime.of(localDate, LocalTime.MIN);
    }

    public static List<String> getETLDateList(LocalDateTime startDateTime, LocalDateTime endDateTime, DateType dateType) {

        List<String> dateList = new ArrayList<>();
        while (startDateTime.isBefore(endDateTime)){
            if(dateType.getValue().equals(TYPE_YEAR)){
                dateList.add(startDateTime.getYear() + "");
                startDateTime = startDateTime.plusYears(1);
            }else if(dateType.getValue().equals(TYPE_MONTH)){
                dateList.add(startDateTime.getYear() + "-" + addZero(startDateTime.getMonthValue()));
                startDateTime = startDateTime.plusMonths(1);
            }else if(dateType.getValue().equals(TYPE_DAY)){
                dateList.add(startDateTime.toLocalDate().toString());
                startDateTime = startDateTime.plusDays(1);
            }else if(dateType.getValue().equals(TYPE_WEEK)){
                String week = DateUtils.getWeek(startDateTime.toLocalDate().toString());
                String weekFirstDay = DateUtils.getWeekFirstDay(week);
                dateList.add(weekFirstDay);
                startDateTime = LocalDateTime.of(LocalDate.parse(weekFirstDay), LocalTime.MIN);
                startDateTime = startDateTime.plusDays(7);
            }
        }

        return dateList;

    }

    public static String getReportDateName(String date, String dateType) {
        String dateName;
        if(TYPE_WEEK.equals(dateType)){
            dateName = DateUtils.getWeekFullColumnByDate(date);
        }else if(TYPE_DAY.equals(dateType)){
            dateName = date.substring(5);
        }else {
            dateName = DateUtils.getNameByCycle(date, dateType);
        }
        return dateName;
    }

    public static List<Map<String, Integer>> getHistoryDateDayList(List<String> dates, String dateType, Integer delay) {

        String nowDate = LocalDate.now().minusDays(delay).toString();
        if(TYPE_MONTH.equals(dateType)){
            dates.removeIf(d -> d.compareTo(DateUtils.getCycMonth(nowDate, 1)) > 0);
        }else {
            dates.removeIf(d -> d.compareTo(nowDate) > 0);
        }

        List<Map<String, Integer>> res = new ArrayList<>();
        for(String date: dates){
            Map<String, Integer> map = new HashMap<>();
            if(TYPE_DAY.equals(dateType)){
                map.put(date, 1);
            }else if(TYPE_WEEK.equals(dateType)){
                map.put(date, DateUtils.getWeekDays(DateUtils.getWeek(date)));
            }else if(TYPE_MONTH.equals(dateType)){
                map.put(date, DateUtils.getMonthDays(date));
            }
            res.add(map);
        }

        return res;
    }
}
